package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.web.response.ResponseEntity;
import com.shiwaixiangcun.monitor.domain.User;
import com.shiwaixiangcun.monitor.dto.UserDto;
import com.shiwaixiangcun.monitor.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Value("${server.host}")
    private String hostDomain;


    @RequestMapping(value = "/login")
    public String login(Model model, @RequestParam(required = false) String error) {
        model.addAttribute("error", error);
        return "login";
    }

    //    @RequestMapping(value = {"index", "/"})
//    public String welcome(Model model) {
//
//        User currentUser = SecurityUtils.getCurrentUser();
//        model.addAttribute("user", currentUser);
//
//        return "welcome";
//    }
    @RequestMapping(value = {"/"})
    public String index(Model model, HttpServletRequest request) {
        return "redirect:" + hostDomain;
    }

    @RequestMapping(value = {"/welcome"})
    public String welcome(Model model, HttpServletRequest request) {
        User currentUser = SecurityUtils.getCurrentUser();
        model.addAttribute("user", currentUser);
        return "welcome";
    }


    @RequestMapping(value = "/logined_user", method = RequestMethod.GET)
    public ResponseEntity getLoginedUser() {
        return new ResponseEntity().success(new UserDto(SecurityUtils.getCurrentUser()));
    }

}
